<!--
 * @Descripttion: HN-SaaS管理系统
 * @version: 1.0.0
 * @Author: 侯建军
 * @Date: 2020-09-05 11:06:50
 * @LastEditors: 侯建军
 * @LastEditTime: 2020-09-09 11:35:44
-->
<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form :model="user" :rules="rules" ref="userForm" label-width="100px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="user.realName"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="user.roleId" placeholder="请选择">
            <el-option v-for="item in roles" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职称" prop="job">
          <el-select v-model="user.job.code" placeholder="请选择">
            <el-option v-for="item in jobs" :key="item.code" :label="item.name" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-select v-model="user.department.code" placeholder="请选择">
            <el-option
              v-for="item in departments"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="user.level.code" placeholder="请选择">
            <el-option
              v-for="item in levels"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles } from "@/api/role.js";
import { getDepartments, getJobs, getLevels } from "@/api/common.js";
import { insert, update } from "@/api/user.js";

export default {
  props: ["dialogVisible", "title", "user"],
  name: "",
  data() {
    return {
      show: false,
      roles: [],
      jobs: [],
      levels: [],
      departments: [],
      rules: {
        username: [
          { required: true, message: "用户名称不可为空！", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不可为空！", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
        realName: [
          { required: true, message: "姓名不可为空！", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        roleId: [{ required: true, message: "角色不可为空！", trigger: "change" }],
      },
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    loadData() {
      // 加载角色
      this.loadRole();
      // 加载科室
      this.loadDepartment();
      //
      this.loadJobs();
      //
      this.loadLevels();
    },
    loadRole() {
      getRoles().then((res) => {
        if (res.data.code === "0000") {
          this.roles = res.data.data;
        }
      });
    },
    loadDepartment() {
      getDepartments().then((res) => {
        if (res.data.code === "0000") {
          this.departments = res.data.data;
        }
      });
    },
    loadJobs() {
      getJobs().then((res) => {
        if (res.data.code === "0000") {
          this.jobs = res.data.data;
        }
      });
    },
    loadLevels() {
      getLevels().then((res) => {
        if (res.data.code === "0000") {
          this.levels = res.data.data;
        }
      });
    },
    handleClose() {
      this.$emit("show", this.dialogVisible);
    },
    save() {
      this.$refs["userForm"].validate((valid) => {
        if (!valid) {
          return false;
        }

        if (this.title == "添加用户") {
          this.user.job = this.user.job.code;
          this.user.department = this.user.department.code;
          this.user.level = this.user.level.code;
          insert(this.user).then((res) => {
            if (res.data.code === "0000") {
              this.$message({
                type: "info",
                message: res.data.msg,
              });
              this.$emit("show", this.dialogVisible);
            }
          });
        } else if (this.title == "编辑用户") {
          this.user.job = this.user.job.code;
          this.user.department = this.user.department.code;
          this.user.level = this.user.level.code;
          update(this.user).then((res) => {
            if (res.data.code === "0000") {
              this.$message({
                type: "info",
                message: res.data.msg,
              });
              this.$emit("show", this.dialogVisible);
            }
          });
        }

      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>